package com.vchello.service.impl.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.vchello.bean.user.Invitation;
import com.vchello.bean.user.User;
import com.vchello.bean.user.UserProgress;
import com.vchello.dao.user.InvitationDao;
import com.vchello.dao.user.UserDao;
import com.vchello.dao.user.UserProgressDao;
import com.vchello.service.user.UserService;
import com.vchello.util.Utility;

/**
 * 
 * @author power
 * 
 */
@Service(value = "userService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

	@Autowired
	private UserDao userDao;
	@Autowired
	private InvitationDao invitationDao;
	@Autowired
	private UserProgressDao userProgressDao;

	@Override
	public User findByOne(int id) {
		return userDao.selectById(id);
	}

	@Override
	public Integer saveUser(User user) {
		return userDao.insert(user);
	}

	@Override
	public Integer updateUser(User user) {
		return userDao.updateById(user);
	}

	@Override
	public boolean deleteUser(User user) {
		try {
			userDao.deleteById(user);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public List<User> queryPage(Page<User> page, User user) {
		Wrapper<User> wrapper = new EntityWrapper<User>(user);
		return userDao.selectPage(page, wrapper);
	}

	@Override
	public List<User> queryList(User user) {
		Wrapper<User> wrapper = new EntityWrapper<User>(user);
		return userDao.selectPage(new Page<User>(), wrapper);
	}

	@Override
	public User findUserByUserName(String userName) {
		return userDao.findUserByUserName(userName);
	}

	@Override
	public User login(String userName, String password) {
		return userDao.login(userName, Utility.md5md5(password));
	}

	@Override
	public User findUserByPhone(String phone) {
		return userDao.findUserByPhone(phone);
	}

	@Override
	public User findUserByEmail(String email) {
		return userDao.findUserByEmail(email);
	}

	@Override
	public User adminLogin(String name, String password) {
		return userDao.adminLogin(name, password);
	}

	@Override
	public User findParentUserByUserId(int userId) {
		Wrapper<Invitation> wrapper = new EntityWrapper<Invitation>();
		wrapper.eq("receive_id", userId).eq("send_status", 2);
		List<Invitation> invitations = invitationDao.selectList(wrapper);
		if(invitations.size()>0){
			return userDao.findUserById(invitations.get(0).getInviteId());
		}
		return null;
	}

	@Override
	public UserProgress findUserProgressById(int id) {
		return userProgressDao.selectById(id);
	}

	@Override
	public List<User> findByPropertis(String colum, Object value) {
		Wrapper<User> wrapper = new EntityWrapper<User>();
		wrapper.eq(colum, value);
		return userDao.selectList(wrapper);
	}

	@Override
	public List<Object[]> findDealman(int organizeId) {
		return invitationDao.findDealman(organizeId);
	}

}
